package com.nullpointer.xchatfinal.object;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class TargetChat {
	private ChatType chatType;
	private String fromID;
	private String name;

	public TargetChat() {
		super();
	}

	public TargetChat(ChatType chatType, String fromID, String name) {
		super();
		this.chatType = chatType;
		this.fromID = fromID;
		this.name = name;
	}

	public TargetChat(User user) {
		this.chatType = ChatType.CHAT_PRIVATE;
		this.fromID = Integer.toString(user.getUserID());
		this.name = user.getName();
	}

	public TargetChat(GroupChat groupChat) {
		this.chatType = ChatType.CHAT_GROUP;
		this.fromID = groupChat.getGroupJID();
		this.name = groupChat.getName();
	}

	public TargetChat(MessageContent messageContent) {
		this.chatType = ChatType.CHAT_PRIVATE;
		this.fromID = String.valueOf(messageContent.getFromUserID());
		this.name = messageContent.getName();
	}

	public TargetChat(DetailedMessage detailedMessage) {
		this(detailedMessage.getMessageContent());
	}

	public TargetChat(HistoryMessage historyMessage) {
		DetailedMessage detailedMessage = historyMessage.getDetailedMessage();
		this.chatType = historyMessage.getChatType();
		this.fromID = detailedMessage.getTargetID();
		this.name = detailedMessage.getTargetName();
	}

	public ChatType getChatType() {
		return chatType;
	}

	public void setChatType(ChatType chatType) {
		this.chatType = chatType;
	}

	public String getFromID() {
		return fromID;
	}

	public void setFromID(String fromID) {
		this.fromID = fromID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toJson() {
		return new JSONSerializer().exclude("*.class").deepSerialize(this);
	}

	public static TargetChat fromJsonToTargetChat(String json) {
		return new JSONDeserializer<TargetChat>().use(null, TargetChat.class)
				.deserialize(json);
	}

	public static String toJsonArray(Collection<TargetChat> collection) {
		return new JSONSerializer().exclude("*.class")
				.deepSerialize(collection);
	}

	public static Collection<TargetChat> fromJsonArrayToTargetChats(String json) {
		return new JSONDeserializer<List<TargetChat>>()
				.use(null, ArrayList.class).use("values", TargetChat.class)
				.deserialize(json);
	}

}
